package cn.zn.modules.appoin.controller;

import cn.zn.framework.common.utils.PageUtils;
import cn.zn.framework.common.utils.Query;
import cn.zn.framework.common.utils.R;
import cn.zn.modules.appoin.entity.AtSpecial;
import cn.zn.modules.appoin.entity.AtSpecialWorkTime;
import cn.zn.modules.appoin.service.AtSpecialService;
import cn.zn.modules.appoin.service.AtSpecialWorkTimeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 则规：个人例外
 * @author zn
 * @email zhunahao@qq.com
 * @date 2020-09-01 14:15:28
 */
@RestController
@RequestMapping("appoin/atspecial")
public class AtSpecialController {

    @Autowired
    AtSpecialService atSpecialService;
    @Autowired
    AtSpecialWorkTimeService atSpecialWorkTimeService;

    /**
     * 列表
     */
    @PostMapping("/list/{ruleId}")
    public R list(@PathVariable("ruleId") String ruleId, @RequestParam Map<String, Object> params) {
        return R.ok().put("page", new PageUtils(atSpecialService.page(new Query<AtSpecial>().getPage(params), new QueryWrapper<AtSpecial>()
                .eq("RULE_ID_", ruleId)
                .orderByDesc("SPECIAL_DATE_"))));
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {

        AtSpecial atSpecial = atSpecialService.getById(id);
        List<AtSpecialWorkTime> specialWorkTimes = atSpecialWorkTimeService.list(new QueryWrapper<AtSpecialWorkTime>()
                .eq("SPECIAL_ID_", id).orderByAsc("START_TIME_"));
        atSpecial.setSpecialWorkTimes(specialWorkTimes);
        return R.ok().put("special", atSpecial);
    }

    /**
     * 新增
     *
     * @param atSpecial
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody AtSpecial atSpecial) {
        atSpecialService.saveSpecial(atSpecial);
        return R.ok();
    }

    /**
     * 更新
     *
     * @param atSpecial
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody AtSpecial atSpecial) {
        atSpecialService.updateSpecial(atSpecial);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R detailDelete(@RequestBody String[] ids) {
        atSpecialService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
